package com.imcrl.core
{
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import starling.display.DisplayObject;
	import starling.display.DisplayObjectContainer;
	import starling.display.Image;
	import starling.display.Sprite;
	import starling.display.Stage;
	import starling.events.Event;
	import starling.events.Touch;
	import starling.events.TouchEvent;
	import starling.events.TouchPhase;
	import starling.utils.transformCoords;
	
	public class StarlingHelper
	{
		private static var sHelperMatrix:Matrix = new Matrix();
		private static var sHelperPoint:Point = new Point();
		
		private static var typeClass:Class;
		public function StarlingHelper()
		{
		}
		
		/**
		 * 
		 * @param stage
		 * @param p
		 * @param type
		 * @return 
		 * 
		 */		
		public static function getObjectsUnderPoint(container:DisplayObjectContainer,p:Point,type:Class):Vector.<DisplayObject>{
			typeClass=type;
			
			var displayObject:DisplayObject;
			var hitObject:DisplayObject;
			var result:Vector.<DisplayObject>=new Vector.<DisplayObject>();
			var numChildren:int=container.numChildren;
			for (var i:int=numChildren-1; i>=0; --i)
			{
				var child:DisplayObject = container.getChildAt(i);
				container.getTransformationMatrix(child, sHelperMatrix);
				transformCoords(sHelperMatrix, p.x, p.y, sHelperPoint);
				if(child is typeClass){
					hitObject = child.hitTest(sHelperPoint);
					if(hitObject)result.push(child);
				}else if(child is DisplayObjectContainer){
					result=result.concat(getObjectsUnderPoint(child as DisplayObjectContainer,p,typeClass));
				}
			}
			
			return result;
		}
		
		
		private static function touchEvent(event:TouchEvent):void{
			var touch:Touch=event.getTouch(curInter.stage);
			if(touch.phase !=TouchPhase.MOVED && touch.phase != TouchPhase.HOVER){
				return;
			}
			var gm:Point=touch.getLocation(curInter.stage);
			var cm:Point=curInter.parent.globalToLocal(gm);
			if(curLockCenter==false){
				mouseCurInter=curInter.globalToLocal(gm);
				curLockCenter=true;
			}
			cm=cm.subtract(mouseCurInter);
			
			if(curBounds && curBounds.containsPoint(cm))
			{
				return;
			}
			curInter.x=cm.x;
			curInter.y=cm.y;
		}
		
		private static var curInter:Sprite;
		private static var curLockCenter:Boolean;
		private static var curBounds:Rectangle;
		private static var mouseCurInter:Point=new Point();
		public static function startDrag(interactive:Sprite,lockCenter:Boolean=false,bounds:Rectangle=null):void{
			if(curInter){
				stopDrag(curInter);
			}
			curInter=interactive;
			curLockCenter=lockCenter;
			curBounds=curBounds;
			mouseCurInter.x=mouseCurInter.y=0;
			curInter.addEventListener(Event.REMOVED_FROM_STAGE,removeHandle);
			curInter.stage.addEventListener(TouchEvent.TOUCH,touchEvent);
		}
		private static function removeHandle(event:Event):void{
			var target:Sprite=event.target as Sprite;
			stopDrag(target);
		}
		
		public static function stopDrag(interactive:Sprite):void{
			if(interactive && interactive==curInter){
				curInter.addEventListener(Event.REMOVED_FROM_STAGE,removeHandle);
				curInter.stage.removeEventListener(TouchEvent.TOUCH,touchEvent);
				curInter=null;
			}
			
		}
		
		public static function clearDrag():void{
			stopDrag(curInter);
		}
		
	}
}